package com.baizhi.listener;

import com.baizhi.entity.Emp;
import com.baizhi.esdao.EmpEsDao;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class EsEmpListener {
    @Autowired
    private EmpEsDao eed;
    @RabbitListener(queues = "empadd")
    public void receiveAdd(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody());
        System.out.println("emp接收到消息："+msg);
        //msg是一个emp的json串，把json串转换为emp对象
        ObjectMapper objectMapper = new ObjectMapper();
        Emp emp = objectMapper.readValue(msg, Emp.class);
        System.out.println(emp);
        eed.insert(emp);
    }

    @RabbitListener(queues = "empdelete")
    public void receiveDelete(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody());
        System.out.println("消息的内容："+msg);

    }
}
